package com.jmz.apt.api.controller;

import com.github.pagehelper.PageInfo;
import com.jmz.apt.api.Helper.RequestHelper;
import com.jmz.apt.api.Helper.ResponseHelper;
import com.jmz.apt.api.Utils.ListUtil;
import com.jmz.apt.api.Utils.ObjectUtil;
import com.jmz.apt.api.dto.impl.AdminRoleDto;
import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.message.request.RequestMessage;
import com.jmz.apt.api.message.request.RequestMessageTemplate;
import com.jmz.apt.api.message.response.ResponseMessage;
import com.jmz.apt.api.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by fanglexue on 2019/11/1.
 */

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController{

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * @Description : {  角色列表  只针对于超级管理员可调用该接口}
     * @Author : create by NanFeng on 2019/11/6  14:18
     */
    @RequestMapping(value="/roleList",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage roleList(@RequestBody RequestMessage requestMessage){
        RequestMessageTemplate req = new RequestMessageTemplate(requestMessage);
        getPage(req);
        List<JmzAdminRole> list = adminRoleService.getRoles();
        PageInfo info = pageInfo(list);

        Integer total = (int)info.getTotal();
        Integer pages = info.getPages();
        List<JmzAdminRole> infoList = info.getList();

        ResponseHelper responseHelper = new ResponseHelper();
        responseHelper.setResponseDataSet("角色列表",infoList);
        responseHelper.setResponseQuery(req.getQuery().getPageno(),req.getQuery().getPagesize(),total,pages);
        responseHelper.setResponseBCode(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
        return responseHelper.getResponseMessageTemplate().getResponseMessage();
    }

    /**
     * @Description : {  删除角色 }
     * @Author : create by NanFeng on 2019/11/6  15:04
     */
    @RequestMapping(value="/deleteRole",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage deleteRole(@RequestBody RequestMessage requestMessage){
        // 解析参数，获取roleIds
        RequestMessageTemplate req = new RequestMessageTemplate(requestMessage);
        Map map = ObjectUtil.toObj(req.getDataSetByKey("roleId").get(0), Map.class);
        String ids = map.get("roleId").toString();
        List<Integer> list = ListUtil.toIntList(ids);

        // 删除角色
        boolean flag = adminRoleService.deleteRole(list);
        if(flag)
            return responseMessageTemplate.setBussResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
        else
            return responseMessageTemplate.setBussResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    /**
     * @Description : {  保存角色相关 }
     * @Author : create by NanFeng on 2019/11/6  15:04
     */
    @RequestMapping(value="/saveRole",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage saveRole(@RequestBody RequestMessage requestMessage){
        RequestHelper requestHelper = new RequestHelper();

        List<List<AdminRoleDto>> lists = requestHelper.mapDataSet(objectMapper,requestMessage, AdminRoleDto.class);

        boolean flag = adminRoleService.saveRole(lists);
        if(flag)
            return responseMessageTemplate.setBussResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
        else
            return responseMessageTemplate.setBussResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    /**
     * @Description : {  通过角色查询角色、用户、资源相关 }
     * @Author : create by NanFeng on 2019/11/6  15:04
     */
    @RequestMapping(value="/getInfoByRole",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage getInfoByRole(@RequestBody RequestMessage requestMessage){
        RequestHelper requestHelper = new RequestHelper();

        List<List<AdminRoleDto>> lists = requestHelper.mapDataSet(objectMapper,requestMessage, AdminRoleDto.class);

        // TODO 该map是否可以优化
        Map map = adminRoleService.getInfoByRole(lists);

        ResponseHelper responseHelper = new ResponseHelper();
        responseHelper.setResponseMapSet("角色参数列表",map);
        responseHelper.setResponseBCode(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
        return responseHelper.getResponseMessageTemplate().getResponseMessage();
    }

    /**
     * @Description : {  修改角色相关 }
     * @Author : create by NanFeng on 2019/11/7  16:10
     */
    @RequestMapping(value="/updateRole",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage updateRole(@RequestBody RequestMessage requestMessage){
        RequestHelper requestHelper = new RequestHelper();

        List<List<AdminRoleDto>> lists = requestHelper.mapDataSet(objectMapper,requestMessage, AdminRoleDto.class);

        boolean flag = adminRoleService.updateRole(lists);
        if(flag)
            return responseMessageTemplate.setBussResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
        else
            return responseMessageTemplate.setBussResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }


}
